package com.shanghaionstar.nav;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.ListView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.mag.poi.FavoriteEntity;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.VehicleLoadingActivity;
import com.shanghaionstar.nav.adapter.NavPoiFavLstInChooseMapAdapter;
import com.shanghaionstar.nav.iview.INavFavListSimpleView;
import com.shanghaionstar.nav.model.MapChoosePoiType;
import com.shanghaionstar.nav.presenter.NavSearchFavSimpleListPresenter;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.List;

/**
 * Created by 6335yz on 2016/7/19.
 */
public class NavFavListSimpleListSimpleFragment extends BaseFragment implements INavFavListSimpleView {


    @FindViewById
    private ProgressLayout loading_progress;
    @FindViewById
    private ListView listview_favorites;
    private NavSearchFavSimpleListPresenter navSearchFavSimpleListPresenter;
    private AbstractMapActivity mapActivity;
    private List<FavoriteEntity> favoriteEntities;
    private NavPoiFavLstInChooseMapAdapter navPoiFavLstInChooseMapAdapter;
    private FragmentManager fragmentManager;
    private MapChoosePoiType mapChoosePoiType;

    @FindViewById
    private ProgressLayout package_list_progress;


    @FindViewById
    private TopLayout top_layout;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mapActivity = (AbstractMapActivity) activity;
        fragmentManager = mapActivity.getSupportFragmentManager();
    }

    @Override
    public int getLayoutId() {
        return R.layout.nav_favorites_list;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        navSearchFavSimpleListPresenter = new NavSearchFavSimpleListPresenter(mapActivity.getScheduler(), mapActivity);
        navSearchFavSimpleListPresenter.setView(this);
        navSearchFavSimpleListPresenter.doGetFavList();
        if (getArguments().getSerializable("mapChoosePoiType") != null) {
            mapChoosePoiType = (MapChoosePoiType) getArguments().getSerializable("mapChoosePoiType");
        }
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
    }

    @Override
    public void showFavData(List<FavoriteEntity> list) {
        if (list != null && list.size() != 0) {
            navPoiFavLstInChooseMapAdapter = new NavPoiFavLstInChooseMapAdapter(list, mapActivity, navSearchFavSimpleListPresenter, mapChoosePoiType);
            listview_favorites.setAdapter(navPoiFavLstInChooseMapAdapter);
        } else {
            AlertDialogUtil.showAlertDialog(mapActivity, getString(R.string.dialog_warning), getString(R.string.nav_get_favorite_empty));
        }
    }

    @Override
    public void setLoading(boolean isLoading) {
        package_list_progress.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showAlertDialog(mapActivity, getString(R.string.dialog_warning), e.getMessage());
    }

    @Override
    public void onReponseSuccess(MapChoosePoiType mapChoosePoiType) {
        switch (mapChoosePoiType) {
            case HOME:
            case COMPANY:
                mapActivity.removeUntilFragment(fragmentManager, NavMainFragment.class, false);
                break;
            case START_POI:
            case END_POI:
                mapActivity.removeUntilFragment(fragmentManager, NavRoutePlanFragment.class, false);
                break;
        }
    }

    @Override
    public Activity onActivity() {
        return null;
    }


    public static NavFavListSimpleListSimpleFragment newInstance(MapChoosePoiType mapChoosePoiType) {
        Bundle bundle = new Bundle();
        NavFavListSimpleListSimpleFragment navFavListSimpleListSimpleFragment = new NavFavListSimpleListSimpleFragment();
        bundle.putSerializable("mapChoosePoiType", mapChoosePoiType);
        navFavListSimpleListSimpleFragment.setArguments(bundle);
        return navFavListSimpleListSimpleFragment;
    }

    @Override
    public void showCommandLoading(boolean isShow) {
        if (isShow) {
            startActivity(new Intent(mapActivity, VehicleLoadingActivity.class));
        }
    }
}
